<?php
class CategoriesController extends AppController
{
	
	var $name = 'Categories';
	var $uses = array('Category', 'Article');
	var $helpers = array('Html', 'Form', 'Javascript');
	var $components = array('Auth','Upload');
	var $layout = 'admin';
	
	function beforeFilter()
	{
		parent::beforeFilter();
		//$this->Auth->allow('*');
	}
	
	function index($id = null)
	{
		$conditions = '1=1';
		$this->Category->recursive = 0;
		$this->paginate = array('limit' => 100, 'page' => 1, 'order' => array("Category.root_path"));
		$this->set ('categories', $this->paginate ('Category', array($conditions)));
	}
	
	function view($id = null)
	{
		if(! $id)
		{
			$this->flash (__ ('Invalid Category', true), array('action' => 'index'));
		}
		$this->set ('category', $this->Category->read (null, $id));
	}
	
	function add() {
		//$categories = $this->Category->find('list', array('field' => array('Category.id', 'Category.name')));
		$categories = $this->News->findOrderList ();
		//debug($categories);
		//die();
		$this->set('categories',$categories);	
		if (!empty($this->data)) {
				//Get Level
				$parent = $this->Category->findById($this->data['Category']['parent_id']);
				$this->data['Category']['level'] = intval($parent['Category']['level']) + 1;
				/*-----end--------*/
				$this->Category->create();	
				if ($this->Category->save($this->data)) {
				//Get root_path
				$category_id = mysql_insert_id();
				$this->Category->id = $category_id;
				$this->Category->saveField('root_path', $parent['Category']['root_path'].$category_id.':');
				/*-----end--------*/

				$this->Session->setFlash(__('The Category has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Category could not be saved. Please, try again.', true));
			}
		}
	}
	
	function edit($id = null) {
		$categories = $this->Category->findOrderList ();
		$this->set('categories',$categories);
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Category', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
				//Edit Level and Rood_path
				$parent = $this->Category->findById($this->data['Category']['parent_id']);
				$this->data['Category']['level'] = intval($parent['Category']['level']) + 1;
				$this->data['Category']['root_path'] = 	$parent['Category']['root_path'].$id.':';
				$this->Category->id = $id;
				/*-----end--------*/
				if ($this->Category->save($this->data)) {
				
				$this->Session->setFlash(__('The Category has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Category could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Category->read(null, $id);
		}
	}
	
	function delete($id = null)
	{
		if(! $id)
		{
			$this->Session->setFlash (__ ('Invalid Category', true));
			$this->redirect (array('action' => 'index'));
		}
		if($this->Category->del ($id))
		{
			$this->Session->setFlash (__ ('Category deleted', true));
			$this->redirect (array('action' => 'index'));
		}
	}
}
?>